package cn.ac.istic.lkt.atv;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.Rectangle;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;

import javax.swing.GroupLayout;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFileChooser;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.GroupLayout.Alignment;

public class FileDlg extends JDialog{

	/**
	 * 
	 */
	private static final long serialVersionUID = -1883463960010382206L;
	FrmAbstract app;
	JTextField ff =null;
	JTextField fe =null;
	JTextField fa =null;
	JButton fb = null;
	JButton eb = null;
	JButton ab = null;
	JButton bok = null;
	JButton bcl = null;
	boolean returnval = false;
	
	public FileDlg(FrmAbstract atv){
		super(atv,true);
		app = atv;
		setTitle("Specify the Tree Files and Alignment File");
		this.setModal(true);
		getContentPane().setLayout(new BorderLayout());
		JPanel jp = new JPanel();
		//GridLayout gridLayout = new GridLayout(4,3);
		//jp.setLayout(gridLayout);

		GroupLayout layout = new GroupLayout(jp);
		jp.setLayout(layout);
		layout.setAutoCreateGaps(true);
		
		JLabel lf = new JLabel("Up Tree File");
		jp.add(lf);
		ff = new JTextField();
		ff.setColumns(20);
		jp.add(ff);
		fb = new JButton("Open");
		fb.addActionListener(new DlgListener(this));
		jp.add(fb);
		
		JLabel le = new JLabel("Down Tree File");
		jp.add(le);
		fe = new JTextField();
		ff.setColumns(20);
		jp.add(fe);
		eb = new JButton("Open");
		eb.addActionListener(new DlgListener(this));
		jp.add(eb);

		JLabel la = new JLabel("Alignment File");
		jp.add(la);
		fa = new JTextField();
		ff.setColumns(20);
		jp.add(fa);
		ab = new JButton("Open");
		ab.addActionListener(new DlgListener(this));
		jp.add(ab);
		
		GroupLayout.SequentialGroup hGroup = layout.createSequentialGroup();
		hGroup.addGroup(layout.createParallelGroup().addComponent(lf).addComponent(le).addComponent(la));
		hGroup.addGroup(layout.createParallelGroup().addComponent(ff).addComponent(fe).addComponent(fa));
		hGroup.addGroup(layout.createParallelGroup().addComponent(fb).addComponent(eb).addComponent(ab));
		layout.setHorizontalGroup(hGroup);
		
		GroupLayout.SequentialGroup vGroup = layout.createSequentialGroup();
		vGroup.addGroup(layout.createParallelGroup(Alignment.BASELINE).addComponent(lf).addComponent(ff).addComponent(fb));
		vGroup.addGroup(layout.createParallelGroup(Alignment.BASELINE).addComponent(le).addComponent(fe).addComponent(eb));
		vGroup.addGroup(layout.createParallelGroup(Alignment.BASELINE).addComponent(la).addComponent(fa).addComponent(ab));
		layout.setVerticalGroup(vGroup);
		
		getContentPane().add(jp,BorderLayout.CENTER);
		
		JPanel jp2 = new JPanel();
		bok = new JButton("OK");
		bok.addActionListener(new DlgListener(this));
		jp2.add(bok);
		bcl = new JButton("Cancel");
		bcl.addActionListener(new DlgListener(this));
		jp2.add(bcl);
		getContentPane().add(jp2,BorderLayout.SOUTH);
	}
	
	public FrmAbstract getApp(){
		return app;
	}
	
	public boolean showDialog() {
        this.pack();
		Rectangle pr = this.getParent().getBounds();		
		Dimension dlgsize = this.getSize();
		this.setBounds((int)(pr.getX()+pr.getWidth()/2-dlgsize.getWidth()/2), 
				(int)(pr.getY()+pr.getHeight()/2-dlgsize.getHeight()/2),
				(int)dlgsize.getWidth(),
				(int)dlgsize.getHeight());
        this.setVisible(true);
        return this.returnval;
    }
	
	class DlgListener implements ActionListener {
		FileDlg container;
		public DlgListener(FileDlg app) {
			container = app;
		}
		public void actionPerformed(ActionEvent e) {
			if (e.getSource().equals(fb) ) {
				ff.setText(getFileName());
			}
			else if (e.getSource().equals(eb) ) {
				fe.setText(getFileName());
			}
			else if (e.getSource().equals(ab) ) {
				fa.setText(getFileName());
			}
			else if (e.getSource().equals(bok)){
				FrmAbstract ap = container.getApp();
				ap.setFfname(ff.getText());
				ap.setEfname(fe.getText());
				ap.setAfname(fa.getText());		
				returnval = true;
				container.dispose();
			}
			else if (e.getSource().equals(bcl)){
				returnval = false;
			}
		}
		
		public String getFileName(){
			JFileChooser chooser = new JFileChooser();
			String lastPath = container.getApp().getLastPath();
			if (lastPath != null) {
				File lastd = new File(lastPath);
				chooser.setCurrentDirectory(lastd);
			}
			chooser.setMultiSelectionEnabled(false);
			int option = chooser.showOpenDialog(container);
			if (option == JFileChooser.APPROVE_OPTION) {
				lastPath = chooser.getCurrentDirectory().getAbsolutePath();
				File f = chooser.getSelectedFile();
				return lastPath+File.separator+f.getName();
			}
			else 
				return "";
		}

	}

}
